using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.Text;

namespace Common.Database
{
    /// <summary>
    /// 
    /// </summary>
    public class  TableFieldValueSearcher
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="stringValue"></param>
        /// <param name="matchType"></param>
        /// <returns></returns>
        public static bool TableFieldContainsStringValue(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, string stringValue, StringMatchType matchType)
        {
            if(string.IsNullOrEmpty(stringValue))
            {
                return false;
            }
            int existCount = 0;
            if(dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15*60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = "=" + FieldDataType.ToOracleDataValue(SqlDbType.NVarChar, stringValue);
                    if(matchType== StringMatchType.RegexMatch)
                    {
                        selectSql2 = " like " +
                                     FieldDataType.ToOracleDataValue(SqlDbType.NVarChar, "%" + stringValue + "%");
                    }
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if(reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch(Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = "=" + FieldDataType.ToMssqlDataValue(SqlDbType.NVarChar, stringValue);
                    if (matchType == StringMatchType.RegexMatch)
                    {
                        selectSql2 = " like " +
                                     FieldDataType.ToMssqlDataValue(SqlDbType.NVarChar, "%" + stringValue + "%");
                    }
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5*60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if(existCount>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="stringValues"></param>
        /// <returns></returns>
        public static bool TableFieldContainsStringValues(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, string[] stringValues)
        {
            if (stringValues ==null || stringValues.Length==0)
            {
                return false;
            }
            int existCount = 0;
            if (dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15 * 60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = " in (";
                    for(int i=0;i<stringValues.Length;i++)
                    {
                        selectSql2 += FieldDataType.ToOracleDataValue(SqlDbType.NVarChar, stringValues[i]);
                        if(i<stringValues.Length-1)
                        {
                            selectSql2 += ",";
                        }
                        else
                        {
                            selectSql2 += ")";
                        }
                    }
                    
                    cmd.CommandText = selectSql1 + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = " in (";
                    for (int i = 0; i < stringValues.Length; i++)
                    {
                        selectSql2 += FieldDataType.ToMssqlDataValue(SqlDbType.NVarChar, stringValues[i]);
                        if (i < stringValues.Length - 1)
                        {
                            selectSql2 += ",";
                        }
                        else
                        {
                            selectSql2 += ")";
                        }
                    }
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5 * 60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (existCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="numberValue"></param>
        /// <returns></returns>
        public static bool TableFieldContainsNumberValue(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, decimal numberValue)
        {
            int existCount = 0;
            if (dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15 * 60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = "=" + FieldDataType.ToOracleDataValue(SqlDbType.Float, numberValue.ToString());
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = "=" + FieldDataType.ToMssqlDataValue(SqlDbType.Float, numberValue.ToString());
                    
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5 * 60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (existCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="numberValues"></param>
        /// <returns></returns>
        public static bool TableFieldContainsNumberValues(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, decimal[] numberValues)
        {
            if (numberValues == null || numberValues.Length == 0)
            {
                return false;
            }
            int existCount = 0;
            if (dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15 * 60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = " in (";
                    for (int i = 0; i < numberValues.Length; i++)
                    {
                        selectSql2 += FieldDataType.ToOracleDataValue(SqlDbType.Float, numberValues[i].ToString());
                        if (i < numberValues.Length - 1)
                        {
                            selectSql2 += ",";
                        }
                        else
                        {
                            selectSql2 += ")";
                        }
                    }

                    cmd.CommandText = selectSql1 + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = " in (";
                    for (int i = 0; i < numberValues.Length; i++)
                    {
                        selectSql2 += FieldDataType.ToMssqlDataValue(SqlDbType.Float, numberValues[i].ToString());
                        if (i < numberValues.Length - 1)
                        {
                            selectSql2 += ",";
                        }
                        else
                        {
                            selectSql2 += ")";
                        }
                    }
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5 * 60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (existCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fromNumberValue"></param>
        /// <param name="toNumberValue"></param>
        /// <returns></returns>
        public static bool TableFieldWithinNumberRange(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, decimal fromNumberValue, decimal toNumberValue)
        {
            if (fromNumberValue > toNumberValue)
            {
                return false;
            }
            int existCount = 0;
            if (dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15 * 60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = ">=" +
                                        FieldDataType.ToOracleDataValue(SqlDbType.Float, fromNumberValue.ToString()) +
                                        " and " + fieldName + "<=" +
                                        FieldDataType.ToOracleDataValue(SqlDbType.Float, toNumberValue.ToString());

                    cmd.CommandText = selectSql1 + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = ">=" +
                                        FieldDataType.ToMssqlDataValue(SqlDbType.Float, fromNumberValue.ToString()) +
                                        " and " + fieldName + "<=" +
                                        FieldDataType.ToMssqlDataValue(SqlDbType.Float, toNumberValue.ToString());
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5 * 60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (existCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="timeValue"></param>
        /// <returns></returns>
        public static bool TableFieldContainsDateTimeValue(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, DateTime timeValue)
        {
            int existCount = 0;
            if (dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15 * 60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = "=" + FieldDataType.ToOracleDataValue(SqlDbType.DateTime, timeValue.ToString());
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = "=" + FieldDataType.ToMssqlDataValue(SqlDbType.DateTime, timeValue.ToString());

                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5 * 60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (existCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="timeValues"></param>
        /// <returns></returns>
        public static bool TableFieldContainsDateTimeValues(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, DateTime[] timeValues)
        {
            if (timeValues == null || timeValues.Length == 0)
            {
                return false;
            }
            int existCount = 0;
            if (dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15 * 60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = " in (";
                    for (int i = 0; i < timeValues.Length; i++)
                    {
                        selectSql2 += FieldDataType.ToOracleDataValue(SqlDbType.DateTime, timeValues[i].ToString());
                        if (i < timeValues.Length - 1)
                        {
                            selectSql2 += ",";
                        }
                        else
                        {
                            selectSql2 += ")";
                        }
                    }

                    cmd.CommandText = selectSql1 + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = " in (";
                    for (int i = 0; i < timeValues.Length; i++)
                    {
                        selectSql2 += FieldDataType.ToMssqlDataValue(SqlDbType.DateTime, timeValues[i].ToString());
                        if (i < timeValues.Length - 1)
                        {
                            selectSql2 += ",";
                        }
                        else
                        {
                            selectSql2 += ")";
                        }
                    }
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5 * 60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (existCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fromTimeValue"></param>
        /// <param name="toTimeValue"></param>
        /// <returns></returns>
        public static bool TableFieldWithinDateTimeRange(DataProviderType dbProviderType, string connStr, string schemaName, string tableName, string fieldName, DateTime fromTimeValue, DateTime toTimeValue)
        {
            if (fromTimeValue > toTimeValue)
            {
                return false;
            }
            int existCount = 0;
            if (dbProviderType == DataProviderType.Oracle)
            {
                OracleConnection conn = new OracleConnection(connStr);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    ((IDbCommand)cmd).CommandTimeout = 15 * 60;
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = ">=" +
                                        FieldDataType.ToOracleDataValue(SqlDbType.DateTime, fromTimeValue.ToString()) +
                                        " and " + fieldName + "<=" +
                                        FieldDataType.ToOracleDataValue(SqlDbType.DateTime, toTimeValue.ToString());

                    cmd.CommandText = selectSql1 + selectSql2;
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    string selectSql1 = "select count (*) from " +
                                        (string.IsNullOrEmpty(schemaName) ? tableName : schemaName + "." + tableName) +
                                        " where " + fieldName;
                    string selectSql2 = ">=" +
                                        FieldDataType.ToMssqlDataValue(SqlDbType.DateTime, fromTimeValue.ToString()) +
                                        " and " + fieldName + "<=" +
                                        FieldDataType.ToMssqlDataValue(SqlDbType.DateTime, toTimeValue.ToString());
                    cmd.CommandText = selectSql1 + " " + selectSql2;
                    cmd.CommandTimeout = 5 * 60;
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        try
                        {
                            existCount = int.Parse(reader[0].ToString());
                        }
                        catch
                        {
                            existCount = 0;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (existCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
